package com.yuxinyang.memory.admin.common;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * 返回协议
 *
 * @author 余秀良
 * @version 1.0
 * <p>date: 2019年08月29日  11:45 </p>
 * <p>address: 四川成都 </p>
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class ResponseProtocol {
    /**
     * 请求id
     */
    private String requestId;
    /**
     * 返回的数据
     */
    private Object data;
    /**
     * 成功还是失败
     */
    @Builder.Default
    private boolean success = false;
    /**
     * 返回的消息
     */
    private String message;
    /**
     * 状态码
     */
    @Builder.Default
    private int code = 200;

    public static ResponseProtocol ok() {
        return ResponseProtocol.builder().message(ResponseMessage.SUCCESS).success(true).code(200).build();
    }

    public static ResponseProtocol ok(String message) {
        return ResponseProtocol.builder().message(message).success(true).code(200).build();
    }

    public static <T> ResponseProtocol ok(String message, T data) {
        return ResponseProtocol.builder().message(data != null ? ResponseMessage.SUCCESS : ResponseMessage.NO_DATA).success(true).code(200).data(data).build();
    }
    public static <T> ResponseProtocol ok(T data) {
        return ResponseProtocol.builder().message(data != null ? ResponseMessage.SUCCESS : ResponseMessage.NO_DATA).success(true).code(200).data(data).build();
    }

    public static ResponseProtocol error(int code, String message) {
        return ResponseProtocol.builder().message(message).success(false).code(code).build();
    }

    public static ResponseProtocol error(String message) {
        return ResponseProtocol.builder().message(message).success(false).code(500).build();
    }

    public static ResponseProtocol error() {
        return ResponseProtocol.builder().message(ResponseMessage.FAIL).code(500).build();
    }
}
